<?php
namespace app\adminapiv3\service;

class Email extends Base
{
    private $filepath = './config/email.conf';

    /**
     * @name: 详情
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-08-14 16:40:32
     */
    function info ()
    {
        if (!file_exists($this->filepath))
        {
            return [];
        }

        $data = file_get_contents($this->filepath);
        $return = json_decode($data, true);

        return $return;
    }

    /**
     * @name 修改
     * @param $smtp_addr 服务器
     * @param $smtp_port 端口
     * @param $email_addr 用户名
     * @param $email_username
     * @param $email_password
     * @return bool
     * @Author wx
     * @date time
     */
    function modify ($data = array())
    {
        //验证参数
        if(empty($data['smtp_addr'])){
            $this->setError($this->FAIL_CODE,'SMTP服务器地址不能为空');
            return false;
        }
        if(empty($data['smtp_port'])){
            $this->setError($this->FAIL_CODE,'SMTP端口不能为空');
            return false;
        }
        if(empty($data['username'])){
            $this->setError($this->FAIL_CODE,'用户名不能为空');
            return false;
        }
        if(empty($data['password'])){
            $this->setError($this->FAIL_CODE,'密码不能为空');
            return false;
        }
        if(empty($data['email_addr'])){
            $this->setError($this->FAIL_CODE,'发送人邮箱不能为空');
            return false;
        }

        $confDir = '../runtime/config';
        if (!file_exists($confDir)) {
            mkdir($confDir, 0777, true);
        }


        file_put_contents($this->filepath, json_encode($data));

        return true;
    }

    /**
     * @name 发送email
     * @Author wx
     * @date 2019/8/16 13:37
     */
    function sendEmail($title = '', $sendContent = '')
    {
        if (!file_exists($this->filepath)) {
            return;
        }
        //获取email配置信息
        $content = file_get_contents($this->filepath);
        $data = json_decode($content,true);

        //dump($data);die;

        $type = 'TXT';

        $email_obj = new \app\common\service\Email($data['smtp_addr'], $data['smtp_port'], true, $data['username'], $data['password']);
        $email_obj->debug = false;
        //发送邮件
        return $email_obj->sendmail($data['email_addr'], $data['username'], $title, $sendContent, $type);

    }
}